package com.apes.scm.stock.transfer.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.service.ProductAccountService;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 功能：耗材领用明细
 ***
 * @author zhuyh
 * @time 10:49
 * @date 2019/11/20
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ConsumableUseItem.ConsumableUse_Name)
public class ConsumableUseItem extends BaseModel {
    public static final String ConsumableUse_Name = "stock_consumableuse_item";

    @ManyToOne(optional = false)
    @JoinColumn(name = "consumableUse_id", referencedColumnName = "id")
    private ConsumableUse consumableUse;

    @Id
    @Column(name = "id")
    @ApesGenerator(code = ConsumableUse_Name, name = "耗材领用明细")
    @Comment("订单明细行编码")
    private String id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品编码")
    private Product product;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位编码")
    private ProductUom uom;
/*

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id", nullable = false)
    private PartyRole supplier; //供应商编码
*/

    @Column(name = "useQty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("领用数量")
    private double useQty = 0;


    @Column(name = "note", length = 120)
    @Comment("备注")
    private String note;

    @Column(name = "origin_line")
    @Comment("来源行号")
    private String originLine;

    @Column(name = "oos", columnDefinition = "number(1,0) default 0")
    @Comment("无库存销售标记")
    private boolean oos = false;


    @Transient
    private Double marketableQty;  // 获取实时库存


    public Double getMarketableQty() {
        if (!consumableUse.getState().equals("sent")) return null;
        ProductAccountService productAccountService = SpringManager.getBean(ProductAccountService.class);
        Map map = productAccountService.getUsableQtyToUom(consumableUse.getLocation(), product, uom, company);
        if (map.isEmpty()) return 0.00;
        return (Double) map.get("marketableQty") + useQty;
    }

}
